package com.sailfish.springbootdemo.dao.db7;


import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.common.DB7TableName;
import com.sailfish.springbootdemo.pojo.db7.TbDataDictionary;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface TbDataDictionaryDao {

    @Select("<script> " +
            "select \n" +
            //"id,parent_id,param_name,param_name_en,param_key,param_type,level,select_level,parent_select_id,tip,remark,create_user,create_time,update_user,update_time,status,sort " +
            "*" +
            " from " + DB7TableName.TB_DATA_DICTIONARY +
            " where 1=1 " +
            "<if test='param_key != null'>" +
            " and (param_key = #{param_key}  or  parent_param_key=#{parent_param_key})" +
            "</if>" +
            "<if test='parent_id != null'>" +
            " and parent_id = #{parent_id}" +
            "</if>" +
            " order by parent_id asc " +
            "</script>")
    List<TbDataDictionary> getDataDictionaryList(TbDataDictionary tbDataDictionary);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_DATA_DICTIONARY +
            " where 1=1 " +
            "<if test='type == \"designType\"'>" +
            " and param_key in ('logInfo','designType','application','description')" +
            "</if>" +
            "<if test='type == \"deviceType\"'>" +
            " and param_key in ('logInfo','deviceType','application','description')" +
            "</if>" +
            " order by parent_id asc,id asc " +
            "</script>")
    List<TbDataDictionary> getDataDictionaryByTemplateType(@Param("type") String type);


    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_DATA_DICTIONARY +
            " order by parent_id asc,id asc " +
            "</script>")
    List<TbDataDictionary> getAllDataDictionaryList();

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_DATA_DICTIONARY +
            " where 1=1 " +
            " and is_Value = #{is_Value}" +
            " order by parent_id asc,id asc " +
            "</script>")
    List<TbDataDictionary> getAllDataDictionaryList2(@Param("is_Value") Integer is_Value);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_DATA_DICTIONARY +
            " where 1=1 " +
            "<if test='param_key != null'>" +
            " and param_key = #{param_key}" +
            "</if>" +
            "<if test='parent_param_key != null'>" +
            " and parent_param_key = #{parent_param_key}" +
            "</if>" +
            "<if test='parent_select_id != null'>" +
            " and parent_select_id = #{parent_select_id}" +
            "</if>" +
            "<if test='param_type != null'>" +
            " and param_type = #{param_type}" +
            "</if>" +
            "<if test='select_level != null'>" +
            " and select_level = #{select_level}" +
            "</if>" +
            " order by parent_id asc,id asc " +
            "</script>")
    List<TbDataDictionary> getDataDictionaryByParamKey(TbDataDictionary tbDataDictionary);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_DATA_DICTIONARY +
            " where 1=1 " +
            " and param_key = #{parent_param_key}" +
            " order by parent_id asc,id asc " +
            "</script>")
    List<TbDataDictionary> getDataDictionaryParentId(@Param("parent_param_key")String parent_param_key);

    @Insert("insert into " + DB7TableName.TB_DATA_DICTIONARY + " " +
            "(" +
            "parent_id," +
            "param_name," +
            "param_name_en," +
            "param_key," +
            "parent_param_key," +
            "param_type," +
            "level," +
            "select_level," +
            "parent_select_id," +
            "tip," +
            "remark," +
            "create_user," +
            "create_time," +
            "status," +
            "sort," +
            "is_value," +
            "is_show" +
            ") " +
            "values " +
            "(" +
            "#{parent_id}," +
            "#{param_name}," +
            "#{param_name_en}," +
            "#{param_key}," +
            "#{parent_param_key}," +
            "#{param_type}," +
            "#{level}," +
            "#{select_level}," +
            "#{parent_select_id}," +
            "#{tip}," +
            "#{remark}," +
            "#{create_user}," +
            "#{create_time}," +
            "#{status}," +
            "#{sort}," +
            "#{is_value}," +
            "#{is_show}"+
            ")")
    Integer insertDataDictionary(TbDataDictionary tbDataDictionary);


    @Insert({
            "<script>",
            "insert into " + DB7TableName.TB_DATA_DICTIONARY + " (",
            "<trim prefix='' suffix='' suffixOverrides=','>",
            "parent_id, ",
            "param_name, ",
            "param_name_en, ",
            "param_key, ",
            "parent_param_key, ",
            "param_type, ",
            "level, ",
            "select_level, ",
            "parent_select_id, ",
            "tip, ",
            "remark, ",
            "create_user, ",
            "create_time, ",
            "status, ",
            "sort, ",
            "is_value,",
            "is_show",
            "</trim>",
            ") values ",
            "<foreach collection='entities' item='item' separator=','>",
            "(",
            "#{item.parent_id}, ",
            "#{item.param_name}, ",
            "#{item.param_name_en}, ",
            "#{item.param_key}, ",
            "#{item.parent_param_key}, ",
            "#{item.param_type}, ",
            "#{item.level}, ",
            "#{item.select_level}, ",
            "#{item.parent_select_id}, ",
            "#{item.tip}, ",
            "#{item.remark}, ",
            "#{item.create_user}, ",
            "#{item.create_time}, ",
            "#{item.status}, ",
            "#{item.sort}, ",
            "#{item.is_value},",
            "#{item.is_show}",
            ")",
            "</foreach>",
            "</script>"
    })
    Integer insertDataDictionaryBatch(@Param("entities") List<TbDataDictionary> tbDataDictionaryList);


    @Update("update " + DB7TableName.TB_DATA_DICTIONARY + " set " +
            "parent_id=#{parent_id}," +
            "param_name=#{param_name}," +
            "param_name_en=#{param_name_en}," +
            "param_key=#{param_key}," +
            "parent_param_key=#{parent_param_key}," +
            "param_type=#{param_type}," +
            "level=#{level}," +
            "select_level=#{select_level}," +
            "parent_select_id=#{parent_select_id}," +
            "tip=#{tip}," +
            "remark=#{remark}," +
            "update_user=#{create_user}," +
            "update_time=#{create_time}," +
            "status=#{status}, " +
            "sort=#{sort}, " +
            "is_value=#{is_value}, " +
            "is_show=#{is_show} "+
            "where id=#{id}")
    Integer updateDataDictionary(TbDataDictionary tbDataDictionary);


    @Update("<script>" +
            "update " + DB7TableName.TB_DATA_DICTIONARY +
            "<set>" +
            "<if test='parent_id != null '>" +
            "    parent_id = #{parent_id}," +
            "</if>" +
            "<if test='param_name != null '>" +
            "    param_name = #{param_name}," +
            "</if>" +
            "<if test='param_name_en != null '>" +
            "    param_name_en = #{param_name_en}," +
            "</if>" +
            "<if test='param_key != null '>" +
            "    param_key = #{param_key}," +
            "</if>" +
            "<if test='parent_param_key != null'>" +
            "    parent_param_key = #{parent_param_key}," +
            "</if>" +
            "<if test='param_type != null'>" +
            "    param_type = #{param_type}," +
            "</if>" +
            "<if test='level != null'>" +
            "    level = #{level}," +
            "</if>" +
            "<if test='select_level != null'>" +
            "    select_level = #{select_level}," +
            "</if>" +
            "<if test='parent_select_id != null'>" +
            "    parent_select_id = #{parent_select_id}," +
            "</if>" +
            "<if test='tip != null'>" +
            "    tip = #{tip}," +
            "</if>" +
            "<if test='remark != null '>" +
            "    remark = #{remark}," +
            "</if>" +
            "<if test='update_user != null '>" +
            "    update_user = #{update_user}," +
            "</if>" +
            "<if test='update_time!= null '>" +
            "    update_time = #{update_time}," +
            "</if>" +
            "<if test='status != null '>" +
            "    status = #{status}," +
            "</if>" +
            "<if test='sort != null '>" +
            "    sort = #{sort}," +
            "</if>" +
            "<if test='is_value != null'>" +
            "    is_value = #{is_value}," +
            "</if>" +
            "<if test='is_show != null'>" +
            "    is_show = #{is_show}" +
            "</if>" +
            "</set>" +
            " where id = #{id} " +
            "</script>")
    Integer updateDataDictionaryBatch(TbDataDictionary tbDataDictionary);

    @Delete("<script>" +
            " delete from " + DB7TableName.TB_DATA_DICTIONARY + " where id=#{id}"+
            "</script>")
    Integer deleteDataDictionary(@Param("id") Integer id);

}
